import api from '../../utils/api.js';
import message from '../../utils/message.js';
import common from '../../utils/common.js';
// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    banner_list:[],
    static_url:'',
    topics:[],
    ids:['JZ_5FiFeOqf4l_3ZFEgS9t4cWGRkevBMMsZrD4DakyM'],
    message:null,
    message_index:-1,
    app_message_list:[],
    topic_index:0

  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  selectTopicIndex:function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      topic_index: index
    })
  },
  onLoad( query ) {

    if(query.scene){
      message.show_error (query.scene);
    }
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

    //this.getCategoryList();
  },
  onShow(){
    var me = this;


    api.getIndex( ( result )=>{
      if(result.status==1){


        if(result.data.tmpl_ids.length>0){
          var ids = [];
          var d = result.data.tmpl_ids;
          for(var i=0;i<d.length ;i++ ){
            ids.push( d[i].pri_tmpl_id);
          }

          console.log(ids);
        }

        me.setData({
          banner_list:result.data.banners,
          gift:result.data.gift,
          topics: result.data.topics,
          category_list: result.data.category_list
          //ids:ids
        })

        if(result.data.tmpl_ids.length>0){
          var ids = [];
          var d = result.data.tmpl_ids;
          for(var i=0;i<d.length ;i++ ){
            ids.push( d[i].pri_tmpl_id);
          }

          console.log(ids);

        }
      }

    })
    this.getAppMessageList();
    common.updateCartNums();    
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  openSubscribeMessage:function(){

    var ids = this.data.ids;
    wx.requestSubscribeMessage({
      tmplIds:ids,
      success:function(res){
        console.log(res);

        if(res.errMsg=="requestSubscribeMessage:ok"){

          //通知服务端  ，用户开启订阅消息通知


          var arr = []
          for( var i in res){

            console.log(i)
            if(i!='errMsg'){
              var t = {}
              t.name= i;
              t.value= res[i]
              arr.push( t)
              
            }
          }



          var val = JSON.stringify(arr);

          api.user.openSubscribeMessage( {ids: val}  , function(response){


          })

        }else{

        }
      },
      fail:function( errMsg , errCode ){

        console.log(errMsg);
        console.log(errCode);
      },
      complete:function(){

      }
    })
  },
  doSearch:function(  ){
    wx.navigateTo({
      url: '/pages/goods/search'
    })

  },
  getAppMessageList:function(){
    var me = this;
    api.getAppMessageList({} , function( response ){

      if(response.status==1){


        var len = response.data.length;

        if(len>0){

          me.setData({
            app_message_list: response.data,
            message: response.data[0],
            message_index: 0
          })

        }else{
          me.setData({
            message: null,
            message_index:-1
          })
        }
        me.setData({
          app_message_list: response.data
        })
      }

    })
  },
  messageCallback:function(  ){
    //消息处理完成后，回调函数，继续处理下一个  

    var len = this.data.app_message_list.length;
    var message_index = this.data.message_index;

    if(message_index==len-1){
      this.setData({
        message:null,
        message_index:-1
      })
    }else{
      message_index++;
      var message = this.data.app_message_list[message_index];
      this.setData({
        message: message,
        message_index: message_index
      })
    }
  },
  getCategoryList:function (params) {
    var me = this;

    api.product.getCategoryList( {} , function (response) {
      if(response.status==1){
        me.setData({
          category_list: response.data
        })
      }else{
        console.log(response.message)
      }
      
    })
    
  },
  doScanQrcode:function () {
    wx.scanCode({
      success:function (res) {
        console.log(res.result)
      },
      fail:function (res) {
        console.log(res)
      }
    })
  }
})
